<template>
  <div class="course-list">
    <div class="course-header">
      <div>
        <el-form
          ref="form"
          label-position="left"
          :model="filterParams"
          :inline="true"
          v-permission="36"
        >
          <el-form-item label="课程名称" prop="courseName">
            <el-input
              v-model="filterParams.courseName"
              placeholder="课程名称"
              style="width: 420px"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="filterParams.status">
              <el-option label="全部" value=""></el-option>
              <el-option label="上架" value="1"></el-option>
              <el-option label="下架" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :disabled="loading" @click="handleFilter"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button
          style="float: right; margin-top: -5px"
          type="primary"
          @click="
            $router.push({
              name: 'course-create',
            })
          "
          v-permission="37"
          >新建课程</el-button
        >
      </div>
    </div>
    <div class="course-body">
      <el-table
        :data="courses"
        v-loading="loading"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column prop="id" label="ID" width="50" align="center">
        </el-table-column>
        <el-table-column prop="courseName" label="课程名称" align="center">
        </el-table-column>
        <el-table-column prop="price" label="价格" align="center">
        </el-table-column>
        <el-table-column prop="sortNum" label="排序" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <i
              class="status"
              :style="{ background: scope.row.status ? '#51cf66' : '#ff6b6b' }"
            ></i>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button v-if="!scope.row.status" type="success" @click="onStateChange(scope.row)" v-permission="33">上架</el-button>
            <el-button v-else type="danger" @click="onStateChange(scope.row)" v-permission="33">下架</el-button>
            <el-button
              @click="
                $router.push({
                  name: 'course-edit',
                  params: {
                    courseId: scope.row.id,
                  },
                })
              "
              v-permission="37"
              >编辑</el-button
            >
            <el-button
              @click="
                $router.push({
                  name: 'course-section',
                  params: {
                    courseId: scope.row.id,
                  },
                })
              "
              v-permission="42"
              >内容管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getQueryCourses, changeState } from '@/services/course'
import { formatMoney } from '@/utils/money'

type Course = FromServer.CourseItem & {
  isStatusLoading: boolean
};

export default Vue.extend({
  name: 'CourseList',
  data () {
    return {
      filterParams: {
        currentPage: 1,
        pageSize: 10000,
        courseName: '',
        status: ''
      },
      courses: [],
      totalCount: 0,
      loading: true
    }
  },

  created () {
    this.loadCourses()
  },

  methods: {
    async loadCourses () {
      this.loading = true
      const { data } = await getQueryCourses(this.filterParams)
      data.data.records.forEach((item: Course) => {
        item.isStatusLoading = false
        item.price = formatMoney(item.price as number)
      })
      this.courses = data.data.records
      this.totalCount = data.data.total
      this.loading = false
    },

    handleFilter () {
      this.filterParams.currentPage = 1
      this.loadCourses()
    },

    async onStateChange (course: Course) {
      course.isStatusLoading = true
      await changeState({
        courseId: course.id,
        status: course.status
      })
      this.$message.success(`${course.status === 0 ? '上架' : '下架'}成功`)
      course.isStatusLoading = false
    }
  }
})
</script>

<style lang="scss" scoped>
.el-card {
  margin-bottom: 20px;
}

.course-header {
  display: flex;
  justify-content: space-between;
}

.course-body {
  .el-table {
    background-color: #fff;
  }

  .status {
    display: inline-block;
    cursor: pointer;
    width: 0.875rem;
    height: 0.875rem;
    vertical-align: middle;
    border-radius: 50%;
  }
}
</style>
